﻿using System;
using System.Net;
using System.Drawing;

namespace CamCommander
{
	public class Camera : ICamera
	{
		public Camera(string url, string userName, string password)
		{
            ((ICamera)this).Url = url;
            ((ICamera)this).UserName = userName;
            ((ICamera)this).Password = password;
		}

        #region Event

        // Create an event for each interface event
		event CameraEventHandler CommandSent;

		event CameraEventHandler ICamera.CommandSent
		{
			// Explicit interface implementation required.
			add
			{
				lock (CommandSent)
				{
					CommandSent += value;
				}
			}
			remove
			{
				lock (CommandSent)
				{
					CommandSent -= value;
				}
			}
		}

        private void RaiseCommandSent()
        {
            //The event will be null until an event handler is actually added to it.
            if (CommandSent != null) //Watch out for NullReferenceException
            {
                //raise the event
                CommandSent(this);
            }
        }

        #endregion

		string ICamera.Url { get; set; }

        string ICamera.UserName { get; set; }
        string ICamera.Password { get; set; }


		void ICamera.Send(string command)
		{
		    var url = ((ICamera) this).Url;
            var credentials = new NetworkCredential(((ICamera)this).UserName, ((ICamera)this).Password);

            //get file from camera
			var webRequest = (HttpWebRequest)WebRequest.Create(url + command);
            webRequest.Credentials = credentials;
		    webRequest.PreAuthenticate = true;

            using(var webResponse = (HttpWebResponse) webRequest.GetResponse())
            {
                RaiseCommandSent();
            }
		}

		/*
		private Image ImageFromWebCam()
        {
            Image imageFromWebCam = null;
            
            //get file from camera
            var webRequest = (HttpWebRequest)WebRequest.Create(CameraSnapshotFileName_From);
            webRequest.Credentials = new NetworkCredential(CamUsr, CamUsrPwd);
            webRequest.PreAuthenticate = true;

            var webResponse = (HttpWebResponse)webReques t.GetResponse();

            if (webResponse.StatusCode == HttpStatusCode.OK)
            {
                var responseStream = webResponse.GetResponseStream();

                var memStream = new MemoryStream();

                int buffCount = 0;
                var respBytesBuff = new byte[2858];
                do
                {
                    // Fill the buffer with data
                    buffCount = responseStream.Read(respBytesBuff, 0, respBytesBuff.Length);

                    if (buffCount != 0)
                    {
                        memStream.Write(respBytesBuff, 0, buffCount);
                        memStream.Flush();
                    }
                } while (buffCount > 0);


                imageFromWebCam = System.Drawing.Image.FromStream(memStream);

                responseStream.Close();
                responseStream.Dispose();
                memStream.Close();
                memStream.Dispose();
            }
	}
	*/
	}
}
